package com.ssy.lingxi.order.model.bo;

import com.ssy.lingxi.common.constant.workflow.WorkflowProcessKindEnum;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 交易流程查询结果
 * @author 万宁
 * @version 2.0.0
 * @date 2021-07-30
 */
public class OrderTradeProcessBO implements Serializable {
    private static final long serialVersionUID = 4751714274018011921L;

    public OrderTradeProcessBO() {
    }

    public OrderTradeProcessBO(String processKey, Integer payTimes, BigDecimal expireHours, Boolean skipFirstStep, Integer processKind, Boolean allProducts, List<PayNodeBO> payNodes, Boolean hasContract, Long contractId) {
        this.processKey = processKey;
        this.payTimes = payTimes;
        this.expireHours = expireHours;
        this.skipFirstStep = skipFirstStep;
        this.processKind = WorkflowProcessKindEnum.parse(processKind);
        this.allProducts = allProducts;
        this.payNodes = CollectionUtils.isEmpty(payNodes) ? new ArrayList<>() : payNodes.stream().filter(payNode -> payNode.getBatchNo().compareTo(0) > 0).sorted(Comparator.comparingInt(PayNodeBO::getBatchNo)).collect(Collectors.toList());
        this.hasContract = hasContract;
        this.contractId = contractId;
    }

    public OrderTradeProcessBO(List<TradeProcessDetailBO> details) {
        details.stream().findFirst().ifPresent(detail -> {
            this.processKey = detail.getProcessKey();
            this.payTimes = detail.getPayTimes();
            this.skipFirstStep = detail.getSkipFirstStep();
            this.processKind = WorkflowProcessKindEnum.parse(detail.getProcessKind());
            this.allProducts = detail.getAllProducts();
            this.hasContract = detail.getHasContract();
            this.contractId = detail.getContractId();
            this.payNodes = CollectionUtils.isEmpty(detail.getPayNodes()) ? new ArrayList<>() : detail.getPayNodes().stream().filter(payNode -> payNode.getBatchNo().compareTo(0) > 0).sorted(Comparator.comparingInt(PayNodeBO::getBatchNo)).collect(Collectors.toList());
            this.expireHours = detail.getExpireHours();
        });
    }

    /**
     * 流程的ProcessKey
     */
    private String processKey;

    /**
     * 流程中支付的次数
     */
    private Integer payTimes;

    /**
     * 订单自动取消时间（小时）
     */
    private BigDecimal expireHours;

    /**
     * 流程启动时，是否执行第一个任务
     */
    private Boolean skipFirstStep;

    /**
     * 流程类型
     */
    private WorkflowProcessKindEnum processKind;

    /**
     * 是否适用所有商品
     */
    private Boolean allProducts;

    /**
     * 支付环节配置
     */
    private List<PayNodeBO> payNodes;

    /**
     * 是否有电子合同
     */
    private Boolean hasContract;

    /**
     * 电子合同Id
     */
    private Long contractId;

    public String getProcessKey() {
        return processKey;
    }

    public void setProcessKey(String processKey) {
        this.processKey = processKey;
    }

    public Integer getPayTimes() {
        return payTimes;
    }

    public void setPayTimes(Integer payTimes) {
        this.payTimes = payTimes;
    }

    public BigDecimal getExpireHours() {
        return expireHours;
    }

    public void setExpireHours(BigDecimal expireHours) {
        this.expireHours = expireHours;
    }

    public Boolean getSkipFirstStep() {
        return skipFirstStep;
    }

    public void setSkipFirstStep(Boolean skipFirstStep) {
        this.skipFirstStep = skipFirstStep;
    }

    public WorkflowProcessKindEnum getProcessKind() {
        return processKind;
    }

    public void setProcessKind(WorkflowProcessKindEnum processKind) {
        this.processKind = processKind;
    }

    public Boolean getAllProducts() {
        return allProducts;
    }

    public void setAllProducts(Boolean allProducts) {
        this.allProducts = allProducts;
    }

    public List<PayNodeBO> getPayNodes() {
        return payNodes;
    }

    public void setPayNodes(List<PayNodeBO> payNodes) {
        this.payNodes = payNodes;
    }

    public Boolean getHasContract() {
        return hasContract;
    }

    public void setHasContract(Boolean hasContract) {
        this.hasContract = hasContract;
    }

    public Long getContractId() {
        return contractId;
    }

    public void setContractId(Long contractId) {
        this.contractId = contractId;
    }
}
